package com.code.test.first.tree;

import java.util.ArrayList;
import java.util.List;

public class TraversalNode {

    public static void main(String[] args) {
        TreeNode left10 = new TreeNode(10, null, null);
        TreeNode right20 = new TreeNode(20, null, null);
        TreeNode left30 = new TreeNode(30, null, null);
        TreeNode right40 = new TreeNode(40, null, null);
        TreeNode left3 = new TreeNode(3, left10, right20);
        TreeNode right5 = new TreeNode(5, left30, right40);
        TreeNode root = new TreeNode(1, left3, right5);

        List<Integer> list = new ArrayList<>();
        traversalFront(root, list);
        System.out.println(list);
        list = new ArrayList<>();
        traversalMid(root, list);
        System.out.println(list);
        list = new ArrayList<>();
        traversalBack(root, list);
        System.out.println(list);
    }

    static void traversalFront(TreeNode cur, List<Integer> list) {
        if (cur == null) {
            return;
        }
        list.add(cur.val);
        traversalFront(cur.left, list);
        traversalFront(cur.right, list);
    }

    static void traversalMid(TreeNode cur, List<Integer> list) {
        if (cur == null) {
            return;
        }
        traversalMid(cur.left, list);
        list.add(cur.val);
        traversalMid(cur.right, list);
    }

    static void traversalBack(TreeNode cur, List<Integer> list) {
        if (cur == null) {
            return;
        }
        traversalBack(cur.left, list);
        traversalBack(cur.right, list);
        list.add(cur.val);
    }
}
